package com.xxxx.manager.controller;

import com.xxxx.common.result.BaseResult;
import com.xxxx.common.result.FileResult;
import com.xxxx.common.result.ShopPageInfo;
import com.xxxx.manager.pojo.Brand;
import com.xxxx.manager.pojo.Goods;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.pojo.GoodsImages;
import com.xxxx.manager.service.*;
import com.xxxx.manager.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;



import java.util.List;



@Controller
@RequestMapping("goods")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsImagesService goodsImagesService;

    @Autowired
    private UploadService uploadService;

    //跳转到 商品分类-列表页
    @RequestMapping("category/list")
    public String categoryList(Model model) {
       /* ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("goods/category/category-list");
        modelAndView.addObject("gcvList",null);
        return modelAndView;*/
        List<GoodsCategory> gcvlist=goodsCategoryService.selectGCategoryList();
        System.out.println(gcvlist);
        model.addAttribute("gcvList",gcvlist);

        return "goods/category/category-list";
    }









    //跳转到 商品分类-新增分类查询所有-顶级分类
    @RequestMapping("category/add")
    public String categoryAdd(Model model) {
        List<GoodsCategory> gclist=goodsCategoryService.selectGCategoryTopList();
        model.addAttribute("gcList",gclist);

        return "goods/category/category-add";
    }


    /**
     * 商品分类-新增分类查询-级联查询
     */

    @RequestMapping("category/{parentId}")
    @ResponseBody
    public List<GoodsCategory> selectCategoryList(@PathVariable Short parentId){
        return goodsCategoryService.selectCategoryByParentId(parentId);
    }


    /**
     * 商品分类-新增分类-保存分类
     */
    @RequestMapping("category/save")
    @ResponseBody
    public BaseResult categorySave(GoodsCategory goodsCategory){
        int result =goodsCategoryService.categorySave(goodsCategory);
        return result>0? BaseResult.success():BaseResult.error();
    }


    /**
     * 商品列表
     */
    @RequestMapping("list")
    public String list01(Model model){
        List<GoodsCategory> gclist=goodsCategoryService.selectCategoryList01(null);
        model.addAttribute("gcList",gclist);

        List<Brand> brandList=brandService.selectBrandList();
        model.addAttribute("brandList",brandList);
        return "goods/goods-list";
    }

    /**
     * 商品列表-页面跳转添加
     */
    @RequestMapping("add")
    public String addgoods01(Model model){
        //查询所有顶级分类
        List<GoodsCategory> gclist=goodsCategoryService.selectGCategoryTopList();
        model.addAttribute("gcList",gclist);

        List<Brand> brandList=brandService.selectBrandList();
        model.addAttribute("brandList",brandList);


        return "goods/goods-add";
    }

    /**
     * 商品列表-页面跳转添加_保存
     */
    @RequestMapping("save")
    @ResponseBody
    public BaseResult goodsSave(Goods goods){

        return goodsService.saveGoods(goods);
    }


    /**
     * 商品列表-相册_保存
     */


    @RequestMapping("images/save")
    @ResponseBody
    public BaseResult saveGoodsImages(@RequestParam MultipartFile file,Integer goodsId){

        FileResult result=uploadService.upload(file);
        if (!StringUtils.isEmpty(result.getFileUrl())){//上传成功
            GoodsImages goodsImages=new GoodsImages();
            goodsImages.setImageUrl(result.getFileUrl());
            goodsImages.setGoodsId(goodsId);
            BaseResult baseResult= goodsImagesService.saveGoodsImages(goodsImages);
            return baseResult;
        }else {
            return BaseResult.error();
        }

    }


    @RequestMapping("listForPage")
    @ResponseBody
    public BaseResult goodslistForPage(Goods goods,Integer pageNum,Integer pageSize){

        return goodsService.goodslistForPage01(goods,pageNum,pageSize);

    }

}
